package com.hex.ds.hdtp.core.web.common.exception;

import com.hex.ds.hdtp.core.web.common.base.ResponseInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.stream.Collectors;


/**
 * @Package: com.hex.ds.hdtp.core.web.common.exception
 * @ClassName GlobalExceptionHandler
 * @Description:
 * @Author: jian.qiao
 * @Date 2023/2/13 10:51
 * @Version v1.0
 **/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {


    /** 默认http状态码：500 */
    private final HttpStatus defaultErrorHttpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
    /** 默认全局错误码 */
    private final String defaultErrorCode = "HDTP-300";
    /** 默认全局参数校验错误码 */
    private final String defaultArgumentNotValidErrorCode = "HDTP-301";

    /**
     * @Method handleException
     * @param e
     * @return org.springframework.http.ResponseEntity<?>
     * @Description  捕获全局异常
     * @Author jian.qiao
     * @Date 2021/8/10 9:30
     * @Version V1.0
     */
    @ExceptionHandler(Throwable.class)
    public ResponseEntity<ResponseInfo> handleException(Throwable e) {
        log.error(e.getMessage(),e);
        return buildResponseEntity(e.getMessage());
    }

    /**
     * @Method methodArgumentNotValidException
     * @param e
     * @return org.springframework.http.ResponseEntity<?>
     * @Description 参数校验异常
     * @Author jian.qiao
     * @Date 2021/8/10 9:31
     * @Version V1.0
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity<ResponseInfo> methodArgumentNotValidException(MethodArgumentNotValidException e) {
        log.error(e.getMessage(),e);
        String message = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining(","));
        return buildResponseEntity(defaultArgumentNotValidErrorCode, message);
    }

    /**
     * @Method buildResponseEntity
     * @param message
     * @return org.springframework.http.ResponseEntity<?>
     * @Description 构造返回结果集
     * @Author jian.qiao
     * @Date 2021/8/10 9:32
     * @Version V1.0
     */
    private ResponseEntity<ResponseInfo> buildResponseEntity(String message) {
        ResponseInfo responseInfo = new ResponseInfo().setCode(defaultErrorCode).setMsg(message);
        return ResponseEntity.status(defaultErrorHttpStatus).body(responseInfo);
    }

    /**
     * @Method buildResponseEntity
     * @param code 编码
	 * @param message 异常信息
     * @return org.springframework.http.ResponseEntity<?>
     * @Description 构造返回结果集
     * @Author jian.qiao
     * @Date 2021/8/10 9:32
     * @Version V1.0
     */
    private ResponseEntity<ResponseInfo> buildResponseEntity(String code, String message) {
        ResponseInfo responseInfo = new ResponseInfo().setCode(code).setMsg(message);
        return ResponseEntity.status(defaultErrorHttpStatus).body(responseInfo);
    }
}
